::@ECHO OFF
SETLOCAL

SET CURRENT_DIR=%~dp0
SET SOURCE=%CURRENT_DIR%.envs\*.*
SET TARGET=%USERPROFILE%\.envs\

:: remove files
IF EXIST %TARGET% (
    DEL /F /Q %TARGET%*.*
)

:: copy files
XCOPY %SOURCE% %TARGET% /Y /S

:: query current user's 'PATH' variable from registry
SET REG_EXE=%SystemRoot%\System32\REG.EXE
FOR /F "usebackq tokens=2*" %%a IN (`%REG_EXE% QUERY HKCU\Environment /v PATH`) DO SET CURRENT_PATH=%%b

:: check 'TARGET' whether in 'PATH' variable
SET existed=N
SET remain=%CURRENT_PATH%
:Loop
FOR /F "delims=; tokens=1*" %%a IN ("%remain%") DO (
    IF %%a==%TARGET% SET existed=Y

    SET remain=%%b
)

IF DEFINED remain GOTO :Loop

IF %existed%==N (
    :: append to current user's %PATH%
    SETX PATH "%TARGET%;%CURRENT_PATH%"
)

PAUSE
